﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.Business;
using Actuals.Common;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;
using System.Data;

namespace Actuals
{
    public partial class ShowSprints : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();

            }
            if (!Page.IsPostBack)
            {
                IBusinessProject businessProject = new BusinessProject();
                IList<IProject> projects = businessProject.GetProjects();
                ddlProject.DataSource = projects;
                ddlProject.DataTextField = "projectDescription";
                ddlProject.DataValueField = "ProjectId";
                ddlProject.DataBind();
                ListItem select = new ListItem();
                select.Value = "0";
                select.Text = "Select";
                ddlProject.Items.Insert(0, select);
            }

        }

        protected void FillRelease(string projectId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> releases = businessRelease.GetReleases(int.Parse(projectId));
            ddlRelease.DataSource = releases;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
            ListItem select = new ListItem();
            select.Value = "0";
            select.Text = "Select";
            ddlRelease.Items.Insert(0, select);
        }

        protected void ddlProject_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            FillRelease(ddlProject.SelectedValue);
        }

        protected void btn_GetSprints_click(object sender, EventArgs e)
        {
            BindGridView();

        }

        protected void BindGridView()
        {

            int projectId = int.Parse(ddlProject.SelectedValue);
            int releaseId = int.Parse(ddlRelease.SelectedValue);

            IBusinessSprint businessSprint = new BusinessSprint();
            gvResults.DataSource = businessSprint.GetSprints(projectId,releaseId);
            gvResults.DataBind();

        }

        protected void btnDeleteSprints_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvResults.Rows.Count; i++)
                {
                    CheckBox cb = (CheckBox)gvResults.Rows[i].FindControl("chkSprint");
                    Label lbl = (Label)gvResults.Rows[i].FindControl("lblSprintId");

                    if (cb.Checked)
                    {
                        string sprintId = string.Empty;
                        if (lbl != null)
                        {
                            sprintId = lbl.Text;

                            IBusinessSprint businessSprint = new BusinessSprint();
                            businessSprint.DeleteSprint(int.Parse(sprintId));
                        }
                    }


                }

                lblMsg.Text = "Deleted";
                BindGridView();





            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;

            }


        }

        protected void gvResults_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sprintId;

            switch (e.CommandName)
            {
                case "EditSprint":
                    sprintId = e.CommandArgument.ToString();
                    ClientScript.RegisterStartupScript(this.GetType(), "ShowUpdateSprints", "ShowUpdateSprints('" + sprintId + "');", true);
                    //Response.Redirect("AddActuals.aspx?Mode=Edit&ActualId=" + actualId);
                    break;
                /* case "DeleteActual":
                     actualId = e.CommandArgument.ToString();
                     BusinessActual businessActual = new BusinessActual();
                     IActual actual = new Actual();
                     actual.ActualId = Int32.Parse(actualId);
                     businessActual.DeleteActual(actual);
                     //ClientScript.RegisterStartupScript(
                     //Response.Redirect("AddActuals.aspx?Mode=Edit&ActualId=" + actualId);))
                     break;*/

            }
        }

        protected string getStatusDesc(object status)
        {
            string _status=string.Empty;
            switch (status.ToString())
            {
                case "0":
                    _status = "ToDo";
                    break;
                case "1":
                    _status = "InProgress";
                    break;
                case "2":
                    _status = "Closed";
                    break;
            }

            return _status;
                

        }

    }
}